// pages/edit/edit.js

import json from '../../data'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lastPosition: {
      x: '',
      y: '',
      moveId: ''
    },
    oper: [],
    initData: [],
    data: json,
    background: '',
    imageWidth: 375,
    imageHeight: 555
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = options.data;
    if(typeof(data) != 'undefinded'){
      data = JSON.parse(data);
      let background = '';
      data.views.filter(v => v.name == 'background').forEach(v => {
        background = v.url;
      })
      this.setData({
        data: data,
        background: background
      })
    }
  },
  change: function(e) {
    this.setData({
      lastPosition: {
        x: e.detail.x,
        y: e.detail.y,
        moveId: e.target.id
      }
    })
  },
  touchEnd: function(e) {
    let lastPosition = this.data.lastPosition;
    this.update(lastPosition);
  },
  touchStart: function(e){

  },
  update: function(last){
    let data = this.data.data;
    data.views.filter(v => v.name == last.moveId).forEach( v => {
      v.top = last.y;
      v.left = last.x;
    });
    this.setData({
      data: data
    })
  },
  finish: function(e){
    const data = this.data.data;
    // let pages = getCurrentPages()  //获取当前页面栈的信息
    // let prevPage = pages[pages.length - 2]   //获取上一个页面
    // prevPage.setData({   //把需要回传的值保存到上一个页面
    //   painting: data
    // });
    // wx.navigateBack({   //然后返回上一个页面
    //   delta: 1
    // })

    wx.redirectTo({
      url: '../poster/poster?painting='+JSON.stringify(data),
    })
  }
  
})